Style Content
MGLSource
@interface MGLSource : NSObject
MGLSource
is an abstract base class for map content sources. A map content
source supplies content to be shown on the map. A source is added to an
MGLStyle
object along with an
MGLForegroundStyleLayer
object. The
foreground style layer defines the appearance of any content supplied by the
source.
Each source defined by the style JSON file is represented at runtime by an
MGLSource
object that you can use to refine the map’s content. You can also
add and remove sources dynamically using methods such as
-[MGLStyle addSource:]
and
-[MGLStyle sourceWithIdentifier:]
.
Create instances of MGLShapeSource
,
MGLComputedShapeSource
,
MGLImageSource
, and the concrete
subclasses of MGLTileSource
(MGLVectorTileSource
and
MGLRasterTileSource
) in order to
use MGLSource
’s
properties and methods. Do not create instances of MGLSource
directly, and do
not create your own subclasses of this class.
Initializing a Source
-
-initWithIdentifier:
Returns a source initialized with an identifier.
After initializing and configuring the source, add it to a map view’s style using the
-[MGLStyle addSource:]
method.Declaration
Objective-C
- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier;
Swift
init(identifier: String)
Parameters
identifier
A string that uniquely identifies the source in the style to which it is added.
Return Value
An initialized source.
Identifying a Source
-
identifier
A string that uniquely identifies the source in the style to which it is added.
Declaration
Objective-C
@property (nonatomic, copy) NSString *_Nonnull identifier;
Swift
var identifier: String { get set }
MGLShapeSource
@interface MGLShapeSource : MGLSource
MGLShapeSource
is a map content source that supplies vector shapes to be
shown on the map. The shapes may be instances of
MGLShape
or
MGLFeature
,
or they may be defined by local or external
GeoJSON code. A shape source is added to an
MGLStyle
object along with an
MGLVectorStyleLayer
object. The
vector style
layer defines the appearance of any content supplied by the shape source. You
can update a shape source by setting its shape
or URL
property.
MGLShapeSource
is optimized for data sets that change dynamically and fit
completely in memory. For large data sets that do not fit completely in memory,
use the MGLComputedShapeSource
or MGLVectorTileSource
class.
Each
geojson
source defined by the style JSON file is represented at runtime by an
MGLShapeSource
object that you can use to refine the map’s content and
initialize new style layers. You can also add and remove sources dynamically
using methods such as
-[MGLStyle addSource:]
and
-[MGLStyle sourceWithIdentifier:]
.
Any vector style layer initialized with a shape source should have a nil
value in its sourceLayerIdentifier
property.
Example
var coordinates: [CLLocationCoordinate2D] = [
CLLocationCoordinate2D(latitude: 37.77, longitude: -122.42),
CLLocationCoordinate2D(latitude: 38.91, longitude: -77.04),
]
let polyline = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count))
let source = MGLShapeSource(identifier: "lines", features: [polyline], options: nil)
mapView.style?.addSource(source)
Related examples
See the Cluster point data,
Use images to
cluster point data, and Add
live data examples to learn how to add data to your map using this
MGLSource
object.
Initializing a Source
-
-initWithIdentifier:URL:options:
Returns a shape source with an identifier, URL, and dictionary of options for the source.
This class supports the following options:
MGLShapeSourceOptionClustered
,MGLShapeSourceOptionClusterRadius
,MGLShapeSourceOptionMaximumZoomLevelForClustering
,MGLShapeSourceOptionMinimumZoomLevel
,MGLShapeSourceOptionMaximumZoomLevel
,MGLShapeSourceOptionBuffer
, andMGLShapeSourceOptionSimplificationTolerance
. Shapes provided by a shape source are not clipped or wrapped automatically.Related examples
See the Add live data example to learn how to add live data to your map by updating the an
MGLShapeSource
object’sURL
property.Declaration
Objective-C
- (nonnull instancetype) initWithIdentifier:(nonnull NSString *)identifier URL:(nonnull NSURL *)url options: (nullable NSDictionary<MGLShapeSourceOption, id> *)options;
Swift
init(identifier: String, url: URL, options: [MGLShapeSourceOption : Any]? = nil)
Parameters
identifier
A string that uniquely identifies the source.
url
An HTTP(S) URL, absolute file URL, or local file URL relative to the current application’s resource bundle.
options
An
NSDictionary
of options for this source.Return Value
An initialized shape source.
-
-initWithIdentifier:shape:options:
Returns a shape source with an identifier, a shape, and dictionary of options for the source.
This class supports the following options:
MGLShapeSourceOptionClustered
,MGLShapeSourceOptionClusterRadius
,MGLShapeSourceOptionMaximumZoomLevelForClustering
,MGLShapeSourceOptionMinimumZoomLevel
,MGLShapeSourceOptionMaximumZoomLevel
,MGLShapeSourceOptionBuffer
, andMGLShapeSourceOptionSimplificationTolerance
. Shapes provided by a shape source are not clipped or wrapped automatically.To specify attributes about the shape, use an instance of an
MGLShape
subclass that conforms to theMGLFeature
protocol, such asMGLPointFeature
. To include multiple shapes in the source, use anMGLShapeCollection
orMGLShapeCollectionFeature
object, or use the-initWithIdentifier:features:options:
or-initWithIdentifier:shapes:options:
methods.To create a shape from GeoJSON source code, use the
+[MGLShape shapeWithData:encoding:error:]
method.Related examples
See the Animate a line example to learn how to animate line data by continously updating an
MGLShapeSource
‘sshape
attribute.Declaration
Objective-C
- (nonnull instancetype) initWithIdentifier:(nonnull NSString *)identifier shape:(nullable MGLShape *)shape options: (nullable NSDictionary<MGLShapeSourceOption, id> *)options;
Swift
init(identifier: String, shape: MGLShape?, options: [MGLShapeSourceOption : Any]? = nil)
Parameters
identifier
A string that uniquely identifies the source.
shape
A concrete subclass of
MGLShape
options
An
NSDictionary
of options for this source.Return Value
An initialized shape source.
-
-initWithIdentifier:features:options:
Returns a shape source with an identifier, an array of features, and a dictionary of options for the source.
This class supports the following options:
MGLShapeSourceOptionClustered
,MGLShapeSourceOptionClusterRadius
,MGLShapeSourceOptionMaximumZoomLevelForClustering
,MGLShapeSourceOptionMinimumZoomLevel
,MGLShapeSourceOptionMaximumZoomLevel
,MGLShapeSourceOptionBuffer
, andMGLShapeSourceOptionSimplificationTolerance
. Shapes provided by a shape source are not clipped or wrapped automatically.Unlike
-initWithIdentifier:shapes:options:
, this method acceptsMGLFeature
instances, such asMGLPointFeature
objects, whose attributes you can use when applying a predicate toMGLVectorStyleLayer
or configuring a style layer’s appearance.To create a shape from GeoJSON source code, use the
+[MGLShape shapeWithData:encoding:error:]
method.Declaration
Objective-C
- (nonnull instancetype) initWithIdentifier:(nonnull NSString *)identifier features:(nonnull NSArray<MGLShape<MGLFeature> *> *)features options: (nullable NSDictionary<MGLShapeSourceOption, id> *)options;
Parameters
identifier
A string that uniquely identifies the source.
features
An array of objects that conform to the MGLFeature protocol.
options
An
NSDictionary
of options for this source.Return Value
An initialized shape source.
-
-initWithIdentifier:shapes:options:
Returns a shape source with an identifier, an array of shapes, and a dictionary of options for the source.
This class supports the following options:
MGLShapeSourceOptionClustered
,MGLShapeSourceOptionClusterRadius
,MGLShapeSourceOptionMaximumZoomLevelForClustering
,MGLShapeSourceOptionMinimumZoomLevel
,MGLShapeSourceOptionMaximumZoomLevel
,MGLShapeSourceOptionBuffer
, andMGLShapeSourceOptionSimplificationTolerance
. Shapes provided by a shape source are not clipped or wrapped automatically.Any
MGLFeature
instance passed into this initializer is treated as an ordinary shape, causing any attributes to be inaccessible to anMGLVectorStyleLayer
when evaluating a predicate or configuring certain layout or paint attributes. To preserve the attributes associated with each feature, use the-initWithIdentifier:features:options:
method instead.To create a shape from GeoJSON source code, use the
+[MGLShape shapeWithData:encoding:error:]
method.Declaration
Objective-C
- (nonnull instancetype) initWithIdentifier:(nonnull NSString *)identifier shapes:(nonnull NSArray<MGLShape *> *)shapes options: (nullable NSDictionary<MGLShapeSourceOption, id> *)options;
Swift
convenience init(identifier: String, shapes: [MGLShape], options: [MGLShapeSourceOption : Any]? = nil)
Parameters
identifier
A string that uniquely identifies the source.
shapes
An array of shapes; each shape is a member of a concrete subclass of MGLShape.
options
An
NSDictionary
of options for this source.Return Value
An initialized shape source.
Accessing a Source’s Content
-
shape
The contents of the source. A shape can represent a GeoJSON geometry, a feature, or a collection of features.
If the receiver was initialized using
-initWithIdentifier:URL:options:
, this property is set tonil
. This property is unavailable until the receiver is passed into-[MGLStyle addSource:]
.You can get/set the shapes within a collection via this property. Actions must be performed on the application’s main thread.
-
URL
The URL to the GeoJSON document that specifies the contents of the source.
If the receiver was initialized using
-initWithIdentifier:shape:options:
, this property is set tonil
.Declaration
Objective-C
@property (nonatomic, copy, nullable) NSURL *URL;
Swift
var url: URL? { get set }
-
-featuresMatchingPredicate:
Returns an array of map features for this source, filtered by the given predicate.
Each object in the returned array represents a feature for the current style and provides access to attributes specified via the
shape
property.Features come from tiled GeoJSON data that is converted to tiles internally, so feature geometries are clipped at tile boundaries and features may appear duplicated across tiles. For example, suppose this source contains a long polyline representing a road. The resulting array includes those parts of the road that lie within the map tiles that the source has loaded, even if the road extends into other tiles. The portion of the road within each map tile is included individually.
Returned features may not necessarily be visible to the user at the time they are loaded: the style may lack a layer that draws the features in question. To obtain only visible features, use the
-[MGLMapView visibleFeaturesAtPoint:inStyleLayersWithIdentifiers:predicate:]
or-[MGLMapView visibleFeaturesInRect:inStyleLayersWithIdentifiers:predicate:]
method.Declaration
Objective-C
- (nonnull NSArray<id<MGLFeature>> *)featuresMatchingPredicate: (nullable NSPredicate *)predicate;
Parameters
predicate
A predicate to filter the returned features. Use
nil
to include all features in the source.Return Value
An array of objects conforming to the
MGLFeature
protocol that represent features in the source that match the predicate. -
-leavesOfCluster:offset:limit:
Returns an array of map features that are the leaves of the specified cluster. (“Leaves” are the original points that belong to the cluster.)
This method supports pagination; you supply an offset (number of features to skip) and a maximum number of features to return.
Declaration
Objective-C
- (nonnull NSArray<id<MGLFeature>> *) leavesOfCluster:(nonnull MGLPointFeatureCluster *)cluster offset:(NSUInteger)offset limit:(NSUInteger)limit;
Parameters
cluster
An object of type
MGLPointFeatureCluster
(that conforms to theMGLCluster
protocol).offset
Number of features to skip.
limit
The maximum number of features to return
Return Value
An array of objects that conform to the
MGLFeature
protocol. -
-childrenOfCluster:
Returns an array of map features that are the immediate children of the specified cluster on the next zoom level. The may include features that also conform to the
MGLCluster
protocol (currently only objects of typeMGLPointFeatureCluster
).Note
The returned array may contain the
cluster
that was passed in, if the next zoom level doesn’t match the zoom level for expanding that cluster. See-[MGLShapeSource zoomLevelForExpandingCluster:]
.Declaration
Objective-C
- (nonnull NSArray<id<MGLFeature>> *)childrenOfCluster: (nonnull MGLPointFeatureCluster *)cluster;
Parameters
cluster
An object of type
MGLPointFeatureCluster
(that conforms to theMGLCluster
protocol).Return Value
An array of objects that conform to the
MGLFeature
protocol. -
-zoomLevelForExpandingCluster:
Returns the zoom level at which the given cluster expands.
Declaration
Objective-C
- (double)zoomLevelForExpandingCluster: (nonnull MGLPointFeatureCluster *)cluster;
Swift
func zoomLevel(forExpanding cluster: MGLPointFeatureCluster) -> Double
Parameters
cluster
An object of type
MGLPointFeatureCluster
(that conforms to theMGLCluster
protocol).Return Value
Zoom level. This should be >= 0; any negative return value should be considered an error.
MGLComputedShapeSource
@interface MGLComputedShapeSource : MGLSource
MGLComputedShapeSource
is a map content source that supplies vector shapes,
one tile at a time, to be shown on the map on demand. You implement a class
conforming to the
MGLComputedShapeSourceDataSource
protocol that returns
instances of MGLShape
or
MGLFeature
, then add a computed shape
source to an
MGLStyle
object along with an
MGLVectorStyleLayer
object. The
vector style
layer defines the appearance of any content supplied by the computed shape
source.
MGLComputedShapeSource
is similar to
MGLShapeSource
but is optimized for
data sets that change dynamically or are too large to fit completely in memory.
It is also useful for data that is divided into tiles in a format other than
Mapbox Vector Tiles. For
GeoJSON data, use the
MGLShapeSource
class.
For static tiles or Mapbox Vector Tiles, use the
MGLVectorTileSource
class.
You can add and remove sources dynamically using methods such as
-[MGLStyle addSource:]
and
-[MGLStyle sourceWithIdentifier:]
.
This class
cannot be represented in a style JSON file; you must add it ot the style at
runtime.
-
-initWithIdentifier:options:
Returns a custom shape data source initialized with an identifier, and a dictionary of options for the source according to the style specification.
This class supports the following options:
MGLShapeSourceOptionMinimumZoomLevel
,MGLShapeSourceOptionMaximumZoomLevel
,MGLShapeSourceOptionBuffer
,MGLShapeSourceOptionSimplificationTolerance
,MGLShapeSourceOptionWrapsCoordinates
, andMGLShapeSourceOptionClipsCoordinates
. Shapes provided by a computed shape source cannot be clustered.Declaration
Objective-C
- (nonnull instancetype) initWithIdentifier:(nonnull NSString *)identifier options: (nullable NSDictionary<MGLShapeSourceOption, id> *)options;
Swift
init(identifier: String, options: [MGLShapeSourceOption : Any]? = nil)
Parameters
identifier
A string that uniquely identifies the source.
options
An
NSDictionary
of options for this source. -
-initWithIdentifier:dataSource:options:
Returns a custom shape data source initialized with an identifier, data source, and a dictionary of options for the source according to the style specification.
This class supports the following options:
MGLShapeSourceOptionMinimumZoomLevel
,MGLShapeSourceOptionMaximumZoomLevel
,MGLShapeSourceOptionBuffer
,MGLShapeSourceOptionSimplificationTolerance
,MGLShapeSourceOptionWrapsCoordinates
, andMGLShapeSourceOptionClipsCoordinates
. Shapes provided by a computed shape source cannot be clustered.Declaration
Objective-C
- (nonnull instancetype) initWithIdentifier:(nonnull NSString *)identifier dataSource:(nonnull id<MGLComputedShapeSourceDataSource>)dataSource options: (nullable NSDictionary<MGLShapeSourceOption, id> *)options;
Swift
convenience init(identifier: String, dataSource: MGLComputedShapeSourceDataSource, options: [MGLShapeSourceOption : Any]? = nil)
Parameters
identifier
A string that uniquely identifies the source.
options
An
NSDictionary
of options for this source. -
-invalidateBounds:
Invalidates all the features and properties intersecting with or contained in the specified bounds. New fetch requests will immediately be invoked on the
MGLComputedShapeSourceDataSource
.Declaration
Objective-C
- (void)invalidateBounds:(MGLCoordinateBounds)bounds;
Swift
func invalidateBounds(_ bounds: MGLCoordinateBounds)
Parameters
bounds
Coordinate bounds to invalidate.
-
-invalidateTileAtX:y:zoomLevel:
Invalidates all the feautres and properties of a given tile. A new fetch request will immediately be invoked on the
MGLComputedShapeSourceDataSource
.Declaration
Objective-C
- (void)invalidateTileAtX:(NSUInteger)x y:(NSUInteger)y zoomLevel:(NSUInteger)zoomLevel;
Swift
func invalidateTileAt(x: UInt, y: UInt, zoomLevel: UInt)
Parameters
x
Tile X coordinate.
y
Tile Y coordinate.
zoomLevel
Tile zoom level.
-
-setFeatures:inTileAtX:y:zoomLevel:
Set a new set of features for a tile. This method can be invkoed from background threads. For best performance, use this method only to update tiles that have already been requested through
MGLComputedShapeSourceDataSource.
Declaration
Objective-C
- (void)setFeatures:(nonnull NSArray<MGLShape<MGLFeature> *> *)features inTileAtX:(NSUInteger)x y:(NSUInteger)y zoomLevel:(NSUInteger)zoomLevel;
Parameters
features
Features for the tile.
x
Tile X coordinate.
y
Tile Y coordinate.
zoomLevel
Tile zoom level.
-
dataSource
An object that implements the
MGLComputedShapeSourceDataSource
protocol that will be queried for tile data.Declaration
Objective-C
@property (nonatomic, weak, nullable) id<MGLComputedShapeSourceDataSource> dataSource;
Swift
weak var dataSource: MGLComputedShapeSourceDataSource? { get set }
-
requestQueue
A queue that calls to the data source will be made on.
Declaration
Objective-C
@property (nonatomic, readonly) NSOperationQueue *_Nonnull requestQueue;
Swift
var requestQueue: OperationQueue { get }
MGLTileSource
@interface MGLTileSource : MGLSource
MGLTileSource
is a map content source that supplies map tiles to be shown on
the map. The location of and metadata about the tiles are defined either by an
option dictionary or by an external file that conforms to the
TileJSON specification.
A tile source is added to an MGLStyle
object
along with one or more
MGLRasterStyleLayer
or
MGLVectorStyleLayer
objects. Use
a style layer to
control the appearance of content supplied by the tile source.
A tile source is also known as a tile set. To learn about the structure of a Mapbox-hosted tile set, view it in Mapbox Studio’s Tilesets editor.
Create instances of
MGLRasterTileSource
and
MGLVectorTileSource
in order
to use MGLTileSource
‘s properties and methods. Do not create instances of
MGLTileSource
directly, and do not create your own subclasses of this class.
Accessing a Source’s Content
-
configurationURL
The URL to the TileJSON configuration file that specifies the contents of the source.
If the receiver was initialized using
-initWithIdentifier:tileURLTemplates:options
, this property is set tonil
.Declaration
Objective-C
@property (nonatomic, copy, readonly, nullable) NSURL *configurationURL;
Swift
var configurationURL: URL? { get }
Accessing Attribution Strings
-
attributionInfos
An array of
MGLAttributionInfo
objects that define the attribution statements to be displayed when the map is shown to the user.By default, this array is empty. If the source is initialized with a configuration URL, this array is also empty until the configuration JSON file is loaded.
Declaration
Objective-C
@property (nonatomic, copy, readonly) NSArray<MGLAttributionInfo *> *_Nonnull attributionInfos;
Swift
var attributionInfos: [MGLAttributionInfo] { get }
MGLRasterTileSource
@interface MGLRasterTileSource : MGLTileSource
MGLRasterTileSource
is a map content source that supplies raster image tiles
to be shown on the map. The location of and metadata about the tiles are
defined either by an option dictionary or by an external file that conforms to
the
TileJSON specification.
A raster tile source is added to an MGLStyle
object along with one or more
MGLRasterStyleLayer
objects. Use
a raster style layer to control the
appearance of content supplied by the raster tile source.
Each
raster
source defined by the style JSON file is represented at runtime by an
MGLRasterTileSource
object that you can use to initialize new style layers. You
can also add and remove sources dynamically using methods such as
-[MGLStyle addSource:]
and
-[MGLStyle sourceWithIdentifier:]
.
Example
let source = MGLRasterTileSource(identifier: "clouds", tileURLTemplates: ["https://example.com/raster-tiles/{z}/{x}/{y}.png"], options: [
.minimumZoomLevel: 9,
.maximumZoomLevel: 16,
.tileSize: 512,
.attributionInfos: [
MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "https://mapbox.com"))
]
])
mapView.style?.addSource(source)
Related examples
See the
Add raster imagery example to learn how to add a
MGLRasterStyleLayer
to your map using an MGLRasterTileSource
.
Initializing a Source
-
-initWithIdentifier:configurationURL:
Returns a raster tile source initialized with an identifier and configuration URL.
After initializing and configuring the source, add it to a map view’s style using the
-[MGLStyle addSource:]
method.The URL may be a full HTTP or HTTPS URL or canonical URL. The URL should point to a JSON file that conforms to the TileJSON specification.
If a Mapbox URL is specified, this source uses a tile size of 256. For all other tilesets, the default value is 512. (See the
MGLTileSourceOptionTileSize
documentation for more information about tile sizes.) If you need to use a tile size other than the default, use the-initWithIdentifier:configurationURL:tileSize:
method.Declaration
Objective-C
- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier configurationURL:(nonnull NSURL *)configurationURL;
Swift
convenience init(identifier: String, configurationURL: URL)
Parameters
identifier
A string that uniquely identifies the source in the style to which it is added.
configurationURL
A URL to a TileJSON configuration file describing the source’s contents and other metadata.
Return Value
An initialized raster tile source.
-
-initWithIdentifier:configurationURL:tileSize:
Returns a raster tile source initialized with an identifier, configuration URL, and tile size.
After initializing and configuring the source, add it to a map view’s style using the
-[MGLStyle addSource:]
method.The URL may be a full HTTP or HTTPS URL or, canonical URL. The URL should point to a JSON file that conforms to the TileJSON specification.
Declaration
Objective-C
- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier configurationURL:(nonnull NSURL *)configurationURL tileSize:(CGFloat)tileSize;
Swift
init(identifier: String, configurationURL: URL, tileSize: CGFloat)
Parameters
identifier
A string that uniquely identifies the source in the style to which it is added.
configurationURL
A URL to a TileJSON configuration file describing the source’s contents and other metadata.
tileSize
The width and height (measured in points) of each tiled image in the raster tile source. See the
MGLTileSourceOptionTileSize
documentation for details.Return Value
An initialized raster tile source.
-
-initWithIdentifier:tileURLTemplates:options:
Returns a raster tile source initialized an identifier, tile URL templates, and options.
Tile URL templates are strings that specify the URLs of the raster tile images to load. See the “Tile URL Templates” guide for information about the format of a tile URL template.
After initializing and configuring the source, add it to a map view’s style using the
-[MGLStyle addSource:]
method.Declaration
Objective-C
- (nonnull instancetype) initWithIdentifier:(nonnull NSString *)identifier tileURLTemplates:(nonnull NSArray<NSString *> *)tileURLTemplates options: (nullable NSDictionary<MGLTileSourceOption, id> *)options;
Swift
init(identifier: String, tileURLTemplates: [String], options: [MGLTileSourceOption : Any]? = nil)
Parameters
identifier
A string that uniquely identifies the source in the style to which it is added.
tileURLTemplates
An array of tile URL template strings. Only the first string is used; any additional strings are ignored.
options
A dictionary containing configuration options. See
MGLTileSourceOption
for available keys and values. Pass innil
to use the default values.Return Value
An initialized tile source.
MGLRasterDEMSource
-
MGLSource
MGLSource
is an abstract base class for map content sources. A map content source supplies content to be shown on the map. A source is added to anMGLStyle
object along with anMGLForegroundStyleLayer
object. The foreground style layer defines the appearance of any content supplied by the source.Each source defined by the style JSON file is represented at runtime by an
MGLSource
object that you can use to refine the map’s content. You can also add and remove sources dynamically using methods such as-[MGLStyle addSource:]
and-[MGLStyle sourceWithIdentifier:]
.Create instances of
See moreMGLShapeSource
,MGLComputedShapeSource
,MGLImageSource
, and the concrete subclasses ofMGLTileSource
(MGLVectorTileSource
andMGLRasterTileSource
) in order to useMGLSource
’s properties and methods. Do not create instances ofMGLSource
directly, and do not create your own subclasses of this class.Declaration
Objective-C
@interface MGLSource : NSObject
Swift
class MGLSource : NSObject
-
MGLShapeSource
MGLShapeSource
is a map content source that supplies vector shapes to be shown on the map. The shapes may be instances ofMGLShape
orMGLFeature
, or they may be defined by local or external GeoJSON code. A shape source is added to anMGLStyle
object along with anMGLVectorStyleLayer
object. The vector style layer defines the appearance of any content supplied by the shape source. You can update a shape source by setting itsshape
orURL
property.MGLShapeSource
is optimized for data sets that change dynamically and fit completely in memory. For large data sets that do not fit completely in memory, use theMGLComputedShapeSource
orMGLVectorTileSource
class.Each
geojson
source defined by the style JSON file is represented at runtime by anMGLShapeSource
object that you can use to refine the map’s content and initialize new style layers. You can also add and remove sources dynamically using methods such as-[MGLStyle addSource:]
and-[MGLStyle sourceWithIdentifier:]
.Any vector style layer initialized with a shape source should have a
nil
value in itssourceLayerIdentifier
property.Example
var coordinates: [CLLocationCoordinate2D] = [ CLLocationCoordinate2D(latitude: 37.77, longitude: -122.42), CLLocationCoordinate2D(latitude: 38.91, longitude: -77.04), ] let polyline = MGLPolylineFeature(coordinates: &coordinates, count: UInt(coordinates.count)) let source = MGLShapeSource(identifier: "lines", features: [polyline], options: nil) mapView.style?.addSource(source)
Related examples
See the Cluster point data, Use images to cluster point data, and Add live data examples to learn how to add data to your map using this
See moreMGLSource
object. -
MGLComputedShapeSource
MGLComputedShapeSource
is a map content source that supplies vector shapes, one tile at a time, to be shown on the map on demand. You implement a class conforming to theMGLComputedShapeSourceDataSource
protocol that returns instances ofMGLShape
orMGLFeature
, then add a computed shape source to anMGLStyle
object along with anMGLVectorStyleLayer
object. The vector style layer defines the appearance of any content supplied by the computed shape source.MGLComputedShapeSource
is similar toMGLShapeSource
but is optimized for data sets that change dynamically or are too large to fit completely in memory. It is also useful for data that is divided into tiles in a format other than Mapbox Vector Tiles. For GeoJSON data, use theMGLShapeSource
class. For static tiles or Mapbox Vector Tiles, use theMGLVectorTileSource
class.You can add and remove sources dynamically using methods such as
See more-[MGLStyle addSource:]
and-[MGLStyle sourceWithIdentifier:]
. This class cannot be represented in a style JSON file; you must add it ot the style at runtime. -
MGLTileSource
MGLTileSource
is a map content source that supplies map tiles to be shown on the map. The location of and metadata about the tiles are defined either by an option dictionary or by an external file that conforms to the TileJSON specification. A tile source is added to anMGLStyle
object along with one or moreMGLRasterStyleLayer
orMGLVectorStyleLayer
objects. Use a style layer to control the appearance of content supplied by the tile source.A tile source is also known as a tile set. To learn about the structure of a Mapbox-hosted tile set, view it in Mapbox Studio’s Tilesets editor.
Create instances of
See moreMGLRasterTileSource
andMGLVectorTileSource
in order to useMGLTileSource
‘s properties and methods. Do not create instances ofMGLTileSource
directly, and do not create your own subclasses of this class. -
MGLRasterTileSource
MGLRasterTileSource
is a map content source that supplies raster image tiles to be shown on the map. The location of and metadata about the tiles are defined either by an option dictionary or by an external file that conforms to the TileJSON specification. A raster tile source is added to anMGLStyle
object along with one or moreMGLRasterStyleLayer
objects. Use a raster style layer to control the appearance of content supplied by the raster tile source.Each
raster
source defined by the style JSON file is represented at runtime by anMGLRasterTileSource
object that you can use to initialize new style layers. You can also add and remove sources dynamically using methods such as-[MGLStyle addSource:]
and-[MGLStyle sourceWithIdentifier:]
.Example
let source = MGLRasterTileSource(identifier: "clouds", tileURLTemplates: ["https://example.com/raster-tiles/{z}/{x}/{y}.png"], options: [ .minimumZoomLevel: 9, .maximumZoomLevel: 16, .tileSize: 512, .attributionInfos: [ MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "https://mapbox.com")) ] ]) mapView.style?.addSource(source)
Related examples
See the Add raster imagery example to learn how to add a
See moreMGLRasterStyleLayer
to your map using anMGLRasterTileSource
.Declaration
Objective-C
@interface MGLRasterTileSource : MGLTileSource
Swift
class MGLRasterTileSource : MGLTileSource
-
MGLRasterDEMSource
MGLRasterDEMSource
is a map content source that supplies rasterized digital elevation model (DEM) tiles to be shown on the map. The location of and metadata about the tiles are defined either by an option dictionary or by an external file that conforms to the TileJSON specification. A raster DEM source is added to anMGLStyle
object along with one or moreMGLHillshadeStyleLayer
objects. Use a hillshade style layer to control the appearance of content supplied by the raster DEM source.Each
raster-dem
source defined by the style JSON file is represented at runtime by anMGLRasterDEMSource
object that you can use to initialize new style layers. You can also add and remove sources dynamically using methods such as-[MGLStyle addSource:]
and-[MGLStyle sourceWithIdentifier:]
.Currently, raster DEM sources only support the format used by Mapbox Terrain-RGB.
Example
let terrainRGBURL = URL(string: "maptiler://sources/terrain-rgb")! let source = MGLRasterDEMSource(identifier: "hills", configurationURL: terrainRGBURL) mapView.style?.addSource(source)
Declaration
Objective-C
@interface MGLRasterDEMSource : MGLRasterTileSource
Swift
class MGLRasterDEMSource : MGLRasterTileSource
-
MGLVectorTileSource
MGLVectorTileSource
is a map content source that supplies tiled vector data in Mapbox Vector Tile format to be shown on the map. The location of and metadata about the tiles are defined either by an option dictionary or by an external file that conforms to the TileJSON specification. A vector tile source is added to anMGLStyle
object along with one or moreMGLVectorStyleLayer
objects. A vector style layer defines the appearance of any content supplied by the vector tile source.MGLVectorTileSource
is optimized for data sets that are too large to fit completely in memory, such as vector tile sets or data sets managed in Mapbox Studio. For GeoJSON data, use theMGLShapeSource
class. For tiled data that changes dynamically, theMGLComputedShapeSource
class may be a suitable alternative.Each
vector
source defined by the style JSON file is represented at runtime by anMGLVectorTileSource
object that you can use to initialize new style layers. You can also add and remove sources dynamically using methods such as-[MGLStyle addSource:]
and-[MGLStyle sourceWithIdentifier:]
.Within each vector tile, each geometric coordinate must lie between −1 × extent and (extent × 2) − 1, inclusive. Any vector style layer initialized with a vector tile source must have a non-
nil
value in itssourceLayerIdentifier
property.Commonly used vector tile sources include Mapbox Streets, Mapbox Terrain, and Mapbox Traffic.
Example
let source = MGLVectorTileSource(identifier: "pois", tileURLTemplates: ["https://example.com/vector-tiles/{z}/{x}/{y}.mvt"], options: [ .minimumZoomLevel: 9, .maximumZoomLevel: 16, .attributionInfos: [ MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "https://mapbox.com")) ] ]) mapView.style?.addSource(source)
Related examples
See the Data-driven circles example to learn how to add data to your map using an
See moreMGLVectorTileSource
object.Declaration
Objective-C
@interface MGLVectorTileSource : MGLTileSource
Swift
class MGLVectorTileSource : MGLTileSource
-
MGLImageSource
MGLImageSource
is a content source that is used for a georeferenced raster image to be shown on the map. The georeferenced image scales and rotates as the user zooms and rotates the map. Images may also be used as icons or patterns in a style layer. To register an image for use as an icon or pattern, use the-[MGLStyle setImage:forName:]
method. To configure a point annotation’s image, use theMGLAnnotationImage
class.The geographic location of the raster image content, supplied with
MGLCoordinateQuad
, can be non-axis aligned.MGLImageSource
supports raster content fromNSURL
,NSImage
(macOS), orUIImage
(iOS). An image source is added to anMGLStyle
object along with one or moreMGLRasterStyleLayer
objects. Use a raster style layer to control the appearance of content supplied by the image source.Each
image
source defined by the style JSON file is represented at runtime by anMGLImageSource
object that you can use to initialize new style layers. You can also add and remove sources dynamically using methods such as-[MGLStyle addSource:]
and-[MGLStyle sourceWithIdentifier:]
.Example
let coordinates = MGLCoordinateQuad( topLeft: CLLocationCoordinate2D(latitude: 46.437, longitude: -80.425), bottomLeft: CLLocationCoordinate2D(latitude: 37.936, longitude: -80.425), bottomRight: CLLocationCoordinate2D(latitude: 37.936, longitude: -71.516), topRight: CLLocationCoordinate2D(latitude: 46.437, longitude: -71.516)) let source = MGLImageSource(identifier: "radar", coordinateQuad: coordinates, url: URL(string: "https://maplibre.org/maplibre-gl-js-docs/assets/radar.gif")!) mapView.style?.addSource(source)
Related examples
See the Add an image example to learn how to add an image to your map using
See moreMGLImageSource
.
MGLVectorTileSource
@interface MGLVectorTileSource : MGLTileSource
MGLVectorTileSource
is a map content source that supplies tiled vector data
in Mapbox Vector Tile format
to be shown on the map. The location of and metadata about the tiles are
defined either by an option dictionary or by an external file that conforms to
the
TileJSON specification.
A vector tile source is added to an MGLStyle
object along with one or more
MGLVectorStyleLayer
objects. A
vector style layer defines the appearance of
any content supplied by the vector tile source.
MGLVectorTileSource
is optimized for data sets that are too large to fit
completely in memory, such as vector tile sets or data sets managed in
Mapbox Studio. For
GeoJSON data, use the
MGLShapeSource
class. For tiled data that changes dynamically, the
MGLComputedShapeSource
class may be a suitable alternative.
Each
vector
source defined by the style JSON file is represented at runtime by an
MGLVectorTileSource
object that you can use to initialize new style layers.
You can also add and remove sources dynamically using methods such as
-[MGLStyle addSource:]
and
-[MGLStyle sourceWithIdentifier:]
.
Within each vector tile, each geometric coordinate must lie between
−1 × extent and
(extent × 2) − 1, inclusive. Any vector style
layer initialized with a vector tile source must have a non-nil
value in its
sourceLayerIdentifier
property.
Commonly used vector tile sources include Mapbox Streets, Mapbox Terrain, and Mapbox Traffic.
Example
let source = MGLVectorTileSource(identifier: "pois", tileURLTemplates: ["https://example.com/vector-tiles/{z}/{x}/{y}.mvt"], options: [
.minimumZoomLevel: 9,
.maximumZoomLevel: 16,
.attributionInfos: [
MGLAttributionInfo(title: NSAttributedString(string: "© Mapbox"), url: URL(string: "https://mapbox.com"))
]
])
mapView.style?.addSource(source)
Related examples
See the
Data-driven circles example to learn how to add data to your map using
an MGLVectorTileSource
object.
Initializing a Source
-
-initWithIdentifier:configurationURL:
Returns a vector tile source initialized with an identifier and configuration URL.
After initializing and configuring the source, add it to a map view’s style using the
-[MGLStyle addSource:]
method.The URL may be a full HTTP or HTTPS URL or a canonical URL. The URL should point to a JSON file that conforms to the TileJSON specification.
Declaration
Objective-C
- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier configurationURL:(nonnull NSURL *)configurationURL;
Swift
init(identifier: String, configurationURL: URL)
Parameters
identifier
A string that uniquely identifies the source in the style to which it is added.
configurationURL
A URL to a TileJSON configuration file describing the source’s contents and other metadata.
Return Value
An initialized vector tile source.
-
-initWithIdentifier:tileURLTemplates:options:
Returns a vector tile source initialized an identifier, tile URL templates, and options.
Tile URL templates are strings that specify the URLs of the vector tiles to load. See the “Tile URL Templates” guide for information about the format of a tile URL template.
After initializing and configuring the source, add it to a map view’s style using the
-[MGLStyle addSource:]
method.Declaration
Objective-C
- (nonnull instancetype) initWithIdentifier:(nonnull NSString *)identifier tileURLTemplates:(nonnull NSArray<NSString *> *)tileURLTemplates options: (nullable NSDictionary<MGLTileSourceOption, id> *)options;
Swift
init(identifier: String, tileURLTemplates: [String], options: [MGLTileSourceOption : Any]? = nil)
Parameters
identifier
A string that uniquely identifies the source in the style to which it is added.
tileURLTemplates
An array of tile URL template strings. Only the first string is used; any additional strings are ignored.
options
A dictionary containing configuration options. See
MGLTileSourceOption
for available keys and values. Pass innil
to use the default values.Return Value
An initialized tile source.
Accessing a Source’s Content
-
-featuresInSourceLayersWithIdentifiers:predicate:
Returns an array of map features loaded by this source, restricted to the given source layers and filtered by the given predicate.
Each object in the returned array represents a feature loaded by the source and provides access to attributes specified as part of the loaded feature. The source loads a feature if the source is added to an
MGLMapView
’s style; that style has a layer that uses the source; and the map view has recently scrolled to the region containing the feature.Features come from tiled vector data that is converted to tiles internally, so feature geometries are clipped at tile boundaries and features may appear duplicated across tiles. For example, suppose part of a lengthy polyline representing a road has recently scrolled into view. The resulting array includes those parts of the road that lie within the map tiles that the source has loaded, even if the road extends into other tiles. The portion of the road within each map tile is included individually.
Returned features may not necessarily be visible to the user at the time they are loaded: the style may contain a layer that forces the source’s tiles to load but filters out the features in question, preventing them from being drawn. To obtain only visible features, use the
-[MGLMapView visibleFeaturesAtPoint:inStyleLayersWithIdentifiers:predicate:]
or-[MGLMapView visibleFeaturesInRect:inStyleLayersWithIdentifiers:predicate:]
method.Declaration
Objective-C
- (nonnull NSArray<id<MGLFeature>> *) featuresInSourceLayersWithIdentifiers: (nonnull NSSet<NSString *> *)sourceLayerIdentifiers predicate:(nullable NSPredicate *)predicate;
Swift
func features(sourceLayerIdentifiers: Set<String>, predicate: NSPredicate?) -> [MGLFeature]
Parameters
sourceLayerIdentifiers
The source layers to include in the query. Only the features contained in these source layers are included in the returned array. This array may not be empty.
predicate
A predicate to filter the returned features. Use
nil
to include all loaded features.Return Value
An array of objects conforming to the
MGLFeature
protocol that represent features loaded by the source that match the predicate.
MGLImageSource
@interface MGLImageSource : MGLSource
MGLImageSource
is a content source that is used for a georeferenced raster
image to be shown on the map. The georeferenced image scales and rotates as the
user zooms and rotates the map. Images may also be used as icons or patterns
in a style layer. To register an image for use as an icon or pattern,
use the
-[MGLStyle setImage:forName:]
method. To configure a point
annotation’s image, use the
MGLAnnotationImage
class.
The geographic location of the raster image content, supplied with
MGLCoordinateQuad
, can be non-axis
aligned.
MGLImageSource
supports raster content from NSURL
,
NSImage
(macOS), or
UIImage
(iOS).
An image source is added to an MGLStyle
object along with one or more
MGLRasterStyleLayer
objects. Use
a raster style layer to control the
appearance of content supplied by the image source.
Each
image
source defined by the style JSON file is represented at runtime by an
MGLImageSource
object that you can use to initialize new style layers. You
can also add and remove sources dynamically using methods such as
-[MGLStyle addSource:]
and
-[MGLStyle sourceWithIdentifier:]
.
Example
let coordinates = MGLCoordinateQuad(
topLeft: CLLocationCoordinate2D(latitude: 46.437, longitude: -80.425),
bottomLeft: CLLocationCoordinate2D(latitude: 37.936, longitude: -80.425),
bottomRight: CLLocationCoordinate2D(latitude: 37.936, longitude: -71.516),
topRight: CLLocationCoordinate2D(latitude: 46.437, longitude: -71.516))
let source = MGLImageSource(identifier: "radar", coordinateQuad: coordinates, url: URL(string: "https://maplibre.org/maplibre-gl-js-docs/assets/radar.gif")!)
mapView.style?.addSource(source)
Related examples
See the
Add an image example to learn how to add an image to your map using
MGLImageSource
.
Initializing a Source
-
-initWithIdentifier:coordinateQuad:URL:
Returns a georeferenced image source with an identifier, coordinates and a URL.
Declaration
Objective-C
- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier coordinateQuad:(MGLCoordinateQuad)coordinateQuad URL:(nonnull NSURL *)url;
Swift
init(identifier: String, coordinateQuad: MGLCoordinateQuad, url: URL)
Parameters
identifier
A string that uniquely identifies the source.
coordinateQuad
the top left, top right, bottom right, and bottom left coordinates for the image.
url
An HTTP(S) URL, absolute file URL, or local file URL relative to the current application’s resource bundle.
Return Value
An initialized shape source.
-
-initWithIdentifier:coordinateQuad:image:
Returns a georeferenced image source with an identifier, coordinates and an image.
Declaration
Objective-C
- (nonnull instancetype)initWithIdentifier:(nonnull NSString *)identifier coordinateQuad:(MGLCoordinateQuad)coordinateQuad image:(nonnull UIImage *)image;
Swift
init(identifier: String, coordinateQuad: MGLCoordinateQuad, image: UIImage)
Parameters
identifier
A string that uniquely identifies the source.
coordinateQuad
The top left, top right, bottom right, and bottom left coordinates for the image.
image
The image to display for the source.
Return Value
An initialized shape source.
Accessing a Source’s Content
-
URL
The URL to the source image.
If the receiver was initialized using
-initWithIdentifier:coordinateQuad:image:
or theimage
property is set, this property is set tonil
.Declaration
Objective-C
@property (nonatomic, copy, nullable) NSURL *URL;
Swift
var url: URL? { get set }
-
image
The source image.
If the receiver was initialized using
-initWithIdentifier:coordinateQuad:URL:
or if theURL
property is set, this property is set tonil
.Declaration
Objective-C
@property (nonatomic, retain, nullable) UIImage *image;
Swift
var image: UIImage? { get set }
-
coordinates
The coordinates at which the corners of the source image will be placed.
Declaration
Objective-C
@property (nonatomic) MGLCoordinateQuad coordinates;
Swift
var coordinates: MGLCoordinateQuad { get set }